﻿using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;

/// <summary>
/// 
/// </summary>
public static class App
{
    /// <summary>
    /// 
    /// </summary>
    private static IConfiguration Conf;

    /// <summary>
    /// 
    /// </summary>
    public static AppSettings Settings;

    /// <summary>
    /// 
    /// </summary>
    public static ILogger Logger;

    /// <summary>
    /// 
    /// </summary>
    public static void Init()
    {

        Conf = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", true, true)
            .Build();

        Settings = Conf.Get<AppSettings>();

        SLoggerProvider sLoggerProvider = new SLoggerProvider(new SLoggerConfiguration());
        Logger = sLoggerProvider.CreateLogger("");

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="action"></param>
    public static void OnConfChange(Action action)
    {

        ChangeToken.OnChange(Conf.GetReloadToken, () =>
        {
            var settings = Conf.Get<AppSettings>();

            if (JsonSerializer.Serialize(Settings).Equals(JsonSerializer.Serialize(settings)) == false)
            {
                Settings = settings;
                action.Invoke();
            }
        });

    }
}

/// <summary>
/// 
/// </summary>
public class AppSettings
{
    /// <summary>
    /// 是否同步删除
    /// </summary>
    public bool IsSyncDelete { get; set; }
    /// <summary>
    /// 需要备份的路径
    /// </summary>
    public string OldPath { get; set; }
    /// <summary>
    /// 备份到的路径
    /// </summary>
    public string NewPath { get; set; }
    /// <summary>
    /// 周期，单位小时
    /// </summary>
    public int Cycle { get; set; }

}
